package pl.edu.agh.pros.renewal;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class PeriodicalExecutorFactory {

	public static Map<Long, PeriodicalExecutor> periodicalExecutors = Collections
			.synchronizedMap(new HashMap<Long, PeriodicalExecutor>());

	public static PeriodicalExecutor getPeriodicalExecutor(Long executionInterval) {
		synchronized (periodicalExecutors) {
			PeriodicalExecutor periodicalExecutor = periodicalExecutors.get(executionInterval);
			if (periodicalExecutor == null) {
				periodicalExecutor = new PeriodicalExecutor(executionInterval);
			}
			return periodicalExecutor;
		}
	}

	public static void stopPeriodicalExecutors() {
		synchronized (periodicalExecutors) {
			for (PeriodicalExecutor pe : periodicalExecutors.values()) {
				pe.stop();
			}
		}
	}
}
